package com.hmall.item.web;

import com.hmall.common.dto.Filters;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.Stock;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "item")


public class ItemController {

    @Autowired
    private IItemService itemService;
    /**
     *
     * 分页查询商品
     * **/
    @GetMapping("/list")
    public PageDTO<Item> list(Integer page, Integer size){
        return  itemService.selectPage(page,size);
    }
    /**
     * 根据id查询商品
     * **/
    @GetMapping("/{id}")

    public  Item getById(@PathVariable("id") Long id){
        Item item = itemService.getById(id);
        return item;
    }
    /**
     * 新增商品
     *
     * **/
    @PostMapping
      public void save(@RequestBody Item item){
        itemService.save(item);
      }

       /**
        * 上架下架商品
        *
        * **/
         @PutMapping("/status/{id}/{status}")
        public  void  status(@PathVariable("id") Long id,@PathVariable("status") Integer status ){

             itemService.status(id,status);

        }
        /**
         *
         * 修改商品信息
         * **/
        @PutMapping
        public  void  update(@RequestBody Item item){
             itemService.updateById(item);
        }
        /**
         * 根据id删除
         *
         * **/
        @DeleteMapping("/{id}")
        public  void  delete(@PathVariable Long id){
             itemService.removeById(id);

        }
        /**
         *
         * 基本搜索功能
         *
         * **/
        @PostMapping("/list")
        public  PageDTO<Item> finds(@RequestBody Filters filters){
            return  itemService.finds(filters);

        }
        /**
         *
         * 查询库存数量
         *
         * **/
        @PostMapping("/stock")
        public  void  stock(@RequestBody Stock stock){
         Item item = new Item();
         item.setId(stock.getItemId());
         item.setStock(stock.getNum());
         itemService.updateById(item);
        }








    }


